package com.aliothservice.catfish.product.data;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.query.Update;

public class Product {
	
	public static Update updateFromProductPending(ProductPending pending) {
		Update update = new Update();
		
		update.set("userId", pending.getUserId());
		update.set("name", pending.getName());
		update.set("category", pending.getCategory());
		update.set("ownerName", pending.getOwnerName());
		update.set("ownerPhoneNumber", pending.getOwnerPhoneNumber());
		update.set("province", pending.getProvince());
		update.set("city", pending.getCity());
		update.set("district", pending.getDistrict());
		update.set("originProvince", pending.getOriginProvince());
		update.set("originCity", pending.getOriginCity());
		update.set("originDistrict", pending.getOriginDistrict());
		update.set("productPhotoId", pending.getProductPhotoId());
		update.set("manInChargeName", pending.getManInChargeName());
		update.set("manInChargePhoneNumber", pending.getManInChargePhoneNumber());
		update.set("lastUpdateTime", new Date());
		
		return update;
	}
	
	public Map<String, Object> toMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("id", this.getId());
		map.put("name", this.getName());
		map.put("category", this.getCategory());
		map.put("ownerName", this.getOwnerName());
		map.put("ownerPhoneNumber", this.getOwnerPhoneNumber());
		map.put("province", this.getProvince());
		map.put("city", this.getCity());
		map.put("district", this.getDistrict());
		map.put("originProvince", this.getOriginProvince());
		map.put("originCity", this.getOriginCity());
		map.put("originDistrict", this.getOriginDistrict());
		map.put("productPhotoId", this.getProductPhotoId());
		map.put("manInChargeName", this.getManInChargeName());
		map.put("manInChargePhoneNumber", this.getManInChargePhoneNumber());
		map.put("status", this.getStatus());
		if (null != this.getDescriptionOfReject()) {
			map.put("descriptionOfReject", this.getDescriptionOfReject());
		}
		DateFormat fullFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		map.put("lastUpdateTime", fullFormatter.format(this.getLastUpdateTime()));
		
		return map;
	}
	
	public Map<String, Object> toSearchableMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("id", this.getId());
		map.put("name", this.getName());
		map.put("category", this.getCategory());
		map.put("province", this.getProvince());
		map.put("city", this.getCity());
		map.put("district", this.getDistrict());
		map.put("originProvince", this.getOriginProvince());
		map.put("originCity", this.getOriginCity());
		map.put("originDistrict", this.getOriginDistrict());
		map.put("productPhotoId", this.getProductPhotoId());
		map.put("manInChargeName", this.getManInChargeName());
		if (this.getManInChargePhoneNumber().length() != 11) {
			map.put("manInChargePhoneNumber", this.getManInChargePhoneNumber());
		} else {
			String maskedPhoneNumber = String.format("%s*****%s", this.getManInChargePhoneNumber().substring(0, 2), this.getManInChargePhoneNumber().substring(7, 10));
			map.put("manInChargePhoneNumber", maskedPhoneNumber);
		}
		map.put("status", this.getStatus());
		if (null != this.getDescriptionOfReject()) {
			map.put("descriptionOfReject", this.getDescriptionOfReject());
		}
		if (null != this.getIndividual()) {
			map.put("individual", this.getIndividual());
		}
		if (null != this.getEnterprise()) {
			map.put("enterprise", this.getEnterprise());
		}
		DateFormat fullFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		map.put("lastUpdateTime", fullFormatter.format(this.getLastUpdateTime()));
		
		return map;
	}
	
	@Id
	private String id;
	
	// who owns this product
	private String userId;
	
	private String name;
	private String category;
	private String ownerName;
	private String ownerPhoneNumber;
	private String province;
	private String city;
	private String district;
	private String originProvince;
	private String originCity;
	private String originDistrict;
	
	private String productPhotoId;
	
	private String manInChargeName;
	private String manInChargePhoneNumber;

	private String status;
	private String descriptionOfReject;
	
	private String individual;
	private String enterprise;
	
	private Date lastUpdateTime;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getOwnerName() {
		return ownerName;
	}

	public void setOwnerName(String ownerName) {
		this.ownerName = ownerName;
	}

	public String getOwnerPhoneNumber() {
		return ownerPhoneNumber;
	}

	public void setOwnerPhoneNumber(String ownerPhoneNumber) {
		this.ownerPhoneNumber = ownerPhoneNumber;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public String getOriginProvince() {
		return originProvince;
	}

	public void setOriginProvince(String originProvince) {
		this.originProvince = originProvince;
	}

	public String getOriginCity() {
		return originCity;
	}

	public void setOriginCity(String originCity) {
		this.originCity = originCity;
	}

	public String getOriginDistrict() {
		return originDistrict;
	}

	public void setOriginDistrict(String originDistrict) {
		this.originDistrict = originDistrict;
	}

	public String getProductPhotoId() {
		return productPhotoId;
	}

	public void setProductPhotoId(String productPhotoId) {
		this.productPhotoId = productPhotoId;
	}

	public String getManInChargeName() {
		return manInChargeName;
	}

	public void setManInChargeName(String manInChargeName) {
		this.manInChargeName = manInChargeName;
	}

	public String getManInChargePhoneNumber() {
		return manInChargePhoneNumber;
	}

	public void setManInChargePhoneNumber(String manInChargePhoneNumber) {
		this.manInChargePhoneNumber = manInChargePhoneNumber;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getDescriptionOfReject() {
		return descriptionOfReject;
	}

	public void setDescriptionOfReject(String descriptionOfReject) {
		this.descriptionOfReject = descriptionOfReject;
	}

	public Date getLastUpdateTime() {
		return lastUpdateTime;
	}

	public void setLastUpdateTime(Date lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}
	
	public String getIndividual() {
		return individual;
	}

	public void setIndividual(String individual) {
		this.individual = individual;
	}

	public String getEnterprise() {
		return enterprise;
	}

	public void setEnterprise(String enterprise) {
		this.enterprise = enterprise;
	}

	public static final String PRODUCT_STATUS_APPROVED = "approved";
	public static final String PRODUCT_STATUS_REJECTED = "rejected";

}
